﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ReBasic.Algorithm
{
    public class AttrEntityExtractor
    {
        public event MainFrame.TripleHandler ProcessEvent;
        public event MainFrame.InfoHandler StateEvent;

        StreamReader sr;
        StreamWriter sw;
        FileStream fsr, fsw;

        int attr_entity_cnt, sent_cnt;

        #region Events
        void OnProcessEvent(int va, int vb, int perc)
        {
            if (ProcessEvent != null)
                ProcessEvent(va, vb, perc);
        }

        void OnStateEvent(string info, bool isOver)
        {
            if (StateEvent != null)
                StateEvent(info, isOver);
        }
        #endregion

        void ExtractAttrEntity(Dictionary<string, int> SubConDict)
        {
            string str, attr, entity;
            int idx_bg, idx_ed;
            while (!sr.EndOfStream)
            {
                str = sr.ReadLine();
                if (str.Length >= 15)
                {
                    idx_bg = str.IndexOf("what is the ");
                    idx_ed = str.IndexOf(" of ");
                }
                else
                    idx_bg = idx_ed = -1;
                if (idx_bg >= 0 && idx_ed > idx_bg)
                {
                    attr = str.Substring(idx_bg + 12, idx_ed - idx_bg - 12);
                    entity = str.Substring(idx_ed + 4);
                    //if (attr.Contains('$') || attr.Contains('#'))
                    //    attr = "";
                    int idx1 = entity.IndexOf('$');
                    int idx2 = entity.IndexOf('#');
                    if (idx1 != -1 || idx2 != -1)
                    {
                        if (idx1 == -1) idx1 = 999999;
                        if (idx2 == -1) idx2 = 999999;
                        entity = entity.Substring(0, Math.Min(idx1, idx2)).Trim();
                    }
                    //if (SubConDict.ContainsKey(entity))
                    //{
                        sw.WriteLine(attr + "\t" + entity);
                        attr_entity_cnt++;
                    //}
                }
                if (++sent_cnt % 100000 == 0)
                    OnProcessEvent(sent_cnt, attr_entity_cnt,
                        (int)(100.0 * sent_cnt / MainFrame.TotalSent));
            }
        }

        /// <summary>
        /// 外部调用函数，获取attr-entity pair
        /// <param name="SubConDict">Sub-concept Dictionary</param>
        /// </summary>
        public void Work(Dictionary<string, int> SubConDict)
        {
            attr_entity_cnt = 0;
            sent_cnt = 0;
            //string YahooSentDsPath = @"F:\Relationship_Extraction\Data\YahooSent_ds.txt";
            //string YahooAttrEntityPath = @"F:\Relationship_Extraction\Data\YahooSent_AttrEntity_NoLimit.txt";
            //fsr = new FileStream(YahooSentDsPath, FileMode.Open);
            //fsw = new FileStream(YahooAttrEntityPath, FileMode.Create);
            fsr = new FileStream(MainFrame.SentDsPath, FileMode.Open);
            fsw = new FileStream(MainFrame.AttrEntityPath, FileMode.Create);
            sr = new StreamReader(fsr, Encoding.UTF8);
            sw = new StreamWriter(fsw, Encoding.UTF8);
            ExtractAttrEntity(SubConDict);
            sr.Close(); sw.Close();
            fsr.Close(); fsw.Close();
            OnStateEvent("Generate AttrEntity completed, with "
                + attr_entity_cnt.ToString() + " AttrEntity pairs.", true);
        }

        /// <summary>
        /// 文件流关闭
        /// </summary>
        public void CloseFile()
        {
            if (sr != null)
            {
                sr.Close();
                fsr.Close();
            }
            if (sw != null)
            {
                sw.Close();
                fsw.Close();
            }
            OnStateEvent("Generate AttrEntity stopped, with "
                 + attr_entity_cnt.ToString() + " AttrEntity pairs.", true);
        }

    }
}
